/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ApplicationData implements Serializable {
	/*Attributions*/
	private static final long serialVersionUID = 1L;
	
	private ArrayList<BasicInfoPoi> basicList;
	
	// Arrays of the different types of POI
	private ArrayList<Attraction> attractions;
	private ArrayList<Monument> monuments;
	private ArrayList<Museum> museums;
	private ArrayList<InformationPoint> informationPoints;
	
	
	// Preferences variables
	private int lang;								//Language of the application
	private boolean [] filterPreferences;			//List of the types of interesting points that the user wishes to see. (Monuments, Attractions, Museums, Information points)
	private int distanceFilter = 5;				//Max distance to be displayed, 15 km by default.
	private boolean distEnabled = true;
	private int unit;								//Type of length unit. 
													//			If unit is 0, than the unit is m/kilometer
													//			If unit is 1, than the unit is feet/mile
													//			If unit is 2, than the unit is yard
	
	/**
	 * Constructor of the class
	 */
	public ApplicationData() {
		super();
		
		//Initializing the filter preferences
		filterPreferences = new boolean[4];
		java.util.Arrays.fill(filterPreferences, true);
		
		//Initializing the type of length unit.
		unit = 0;
		
		
	}
	
	/////////////////////////////////////////////////////////////////////
	// GETTERS AND SETTERS
	/////////////////////////////////////////////////////////////////////
	public int getUnit() {
		return unit;
	}

	public void setUnit(int unit) {
		this.unit = unit;
	}
	
	public int getLang() {
		return lang;
	}

	public void setLang(int lang) {
		this.lang = lang;
	}
	
	public boolean[] getFilterPreferences() {
		return filterPreferences;
	}

	public void setFilterPreferences(boolean[] filterPreferences) {
		this.filterPreferences = filterPreferences;
	}
	
	public int getDistfilter(){
		return distanceFilter;
	}
	
	public void setDistfilter(int newFilter){
		distanceFilter = newFilter;
	}
	
	public boolean isDistancefilterEnabled(){
		return distEnabled;
	}
	
	public void DistancefilterEnabled(boolean nVal){
		distEnabled = nVal;
	}
	
	public ArrayList<Attraction> getAttractions() {
		return attractions;
	}

	public void setAttractions(ArrayList<Attraction> attractions) {
		this.attractions = attractions;
	}

	public ArrayList<Monument> getMonuments() {
		return monuments;
	}

	public void setMonuments(ArrayList<Monument> monuments) {
		this.monuments = monuments;
	}

	public ArrayList<Museum> getMuseums() {
		return museums;
	}

	public void setMuseums(ArrayList<Museum> museums) {
		this.museums = museums;
	}

	public ArrayList<InformationPoint> getInformationPoints() {
		return informationPoints;
	}

	public void setInformationPoints(ArrayList<InformationPoint> informationPoints) {
		this.informationPoints = informationPoints;
	}	
	
	public ArrayList<BasicInfoPoi> getBasicList() {
		return basicList;
	}

	public void setBasicList(ArrayList<BasicInfoPoi> basicList) {
		this.basicList = basicList;
	}

	/**
	 * Fills de lists with POIs for each category.
	 * 
	 * @param lista
	 */
	public void setLists(ArrayList<Poi> lista) {
		
		// Initialization of the point of interest lists
		attractions = new ArrayList<Attraction>();
		monuments = new ArrayList<Monument>();
		museums = new ArrayList<Museum>();
		informationPoints = new ArrayList<InformationPoint>();
		basicList = new ArrayList<BasicInfoPoi>();
		
		for (int i = 0; i < lista.size(); ++i) {
			int categoria = lista.get(i).getType();
			
			BasicInfoPoi b = new BasicInfoPoi(lista.get(i).getName(), categoria, lista.get(i), lista.get(i).getImage());
			basicList.add(b);
			
			switch(categoria){
				case 1:
					monuments.add((Monument) lista.get(i));
					break;
				case 2:
					attractions.add((Attraction) lista.get(i));
					break;
				case 3:
					museums.add((Museum) lista.get(i));
					break;
				case 4:
					informationPoints.add((InformationPoint) lista.get(i));
					break;
				default:
					break;
			}
		}	
	}

	
	/**
	 * Searches for a text in the Attractions. 
	 * @param text
	 * @return ArrayList<Attraction>
	 */
	public ArrayList<Attraction> searchInAttractions(String text){
		ArrayList<Attraction> result = new ArrayList<Attraction>();
		if (text.equals("")) {
			result = attractions;
		} else {
			for(int i = 0; i < attractions.size(); i++){
				StringTokenizer tokens = new StringTokenizer(attractions.get(i).getName());
				while(tokens.hasMoreTokens()){
					if(tokens.nextToken().toLowerCase().startsWith(text.toLowerCase())){
						result.add(attractions.get(i));
					}
				}
			}
		}
		return result;
	}
	
	/**
	 * Searches for a text in the Monuments 
	 * @param text
	 * @return ArrayList<Monument>
	 */
	public ArrayList<Monument> searchInMonuments(String text){
		ArrayList<Monument> result = new ArrayList<Monument>();
		if (text.equals("")) {
			result = monuments;
		} else {
			for(int i = 0; i < monuments.size(); i++){
				StringTokenizer tokens = new StringTokenizer(monuments.get(i).getName());
				while(tokens.hasMoreTokens()){
					if(tokens.nextToken().toLowerCase().startsWith(text.toLowerCase())){
						result.add(monuments.get(i));
					}
				}
			}
		}
		return result;
	}
	
	/**
	 * Searches for a text in the Museums 
	 * @param text
	 * @return ArrayList<Museum>
	 */
	public ArrayList<Museum> searchInMuseums(String text){
		ArrayList<Museum> result = new ArrayList<Museum>();
		if (text.equals("")) {
			result = museums;
		} else {
			for(int i = 0; i < museums.size(); i++){
				StringTokenizer tokens = new StringTokenizer(museums.get(i).getName());
				while(tokens.hasMoreTokens()){
					if(tokens.nextToken().toLowerCase().startsWith(text.toLowerCase())){
						result.add(museums.get(i));
					}
				}
			}
		}
		return result;
	}
	
	/**
	 * Searches for a text in the Information Points 
	 * @param text
	 * @return ArrayList<InformationPoint>
	 */
	public ArrayList<InformationPoint> searchInInformationPoints(String text){
		ArrayList<InformationPoint> result = new ArrayList<InformationPoint>();
		if (text.equals("")) {
			result = informationPoints;
		} else {
			for(int i = 0; i < informationPoints.size(); i++){
				StringTokenizer tokens = new StringTokenizer(informationPoints.get(i).getName());
				while(tokens.hasMoreTokens()){
					if(tokens.nextToken().toLowerCase().startsWith(text.toLowerCase())){
						result.add(informationPoints.get(i));
					}
				}
			}
		}
		return result;
	}
	
	/**
	 * Searches for a text in all POIs
	 * @param text
	 * @return ArrayList<BasicInfoPoi>
	 */
	public ArrayList<BasicInfoPoi> searchInAll(String text){
		ArrayList<BasicInfoPoi> result = new ArrayList<BasicInfoPoi>();
		if (text.equals("")) {
			result = basicList;
		} else {
			for(int i = 0; i < basicList.size(); i++){
				StringTokenizer tokens = new StringTokenizer(basicList.get(i).getName());
				while(tokens.hasMoreTokens()){
					if(tokens.nextToken().toLowerCase().startsWith(text.toLowerCase())){
						result.add(basicList.get(i));
					}
				}
			}
		}
		return result;
	}
	
	
	
	
	public Poi searchId(int id){
		for(int i = 0; i < attractions.size(); i++){
			if(attractions.get(i).getId() == id){
				return attractions.get(i);
			}
		}
		for(int i = 0; i < monuments.size(); i++){
			if(monuments.get(i).getId() == id){
				return monuments.get(i);
			}
		}
		for(int i = 0; i < museums.size(); i++){
			if(museums.get(i).getId() == id){
				return museums.get(i);
			}
		}
		for(int i = 0; i < informationPoints.size(); i++){
			if(informationPoints.get(i).getId() == id){
				return informationPoints.get(i);
			}
		}
		return null;
	}

	public Attraction getAttractionName (String name) {
		Attraction at = null;
		for (int i = 0; i < attractions.size(); ++i) {
			if (attractions.get(i).getName().equals(name)){
				at = attractions.get(i);
			}
		}
		return at;
	}

	public Monument getMonumentName (String name) {
		for (int i = 0; i < monuments.size(); ++i) {
			if (monuments.get(i).getName().equals(name)) return monuments.get(i);
		}
		return null;
	}

	public Museum getMuseumName (String name) {
		for (int i = 0; i < museums.size(); ++i) {
			if (museums.get(i).getName().equals(name)) return museums.get(i);
		}
		return null;
	}

	public InformationPoint getInformationPointName (String name) {
		for (int i = 0; i < informationPoints.size(); ++i) {
			if (informationPoints.get(i).getName().equals(name)) return informationPoints.get(i);
		}
		return null;
	}
	
}





